package com.cs.csworkflow.controller;

import com.cs.common.enums.ReturnEnum;
import com.cs.common.vo.ReturnVo;
import com.cs.csworkflow.dbo.FormMain;
import com.cs.csworkflow.dbo.FormNode;
import com.cs.csworkflow.enums.FormMainStatusEnum;
import com.cs.csworkflow.service.FormService;
import com.cs.csworkflow.vo.FindForm;
import com.cs.csworkflow.vo.FormNodes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/form")
public class FormController {
    @Autowired
    private FormService formService;

    /**
     * 新建会办单，只写入主要内容，不涉及会办流程
     */
    @RequestMapping("/save")
    public ReturnVo save(@RequestBody FormMain formMain) {
        ReturnVo returnVo = new ReturnVo();
        FormMain retForm = formService.save(formMain);
        if (null == retForm) {
            return returnVo.failure(ReturnEnum.ERROR.getMessage());
        }
        return returnVo.success(ReturnEnum.SUCCESS.getMessage(), retForm);
    }

    /**
     * 提交表单
     */
    @RequestMapping("/commit")
    public ReturnVo commit(@RequestParam Long formId) {
        ReturnVo returnVo = new ReturnVo();
        FormMain retForm = formService.updateStatus(formId, FormMainStatusEnum.SUBMIT.getCode());
        if (null == retForm) {
            return returnVo.failure(ReturnEnum.ERROR.getMessage());
        }
        return returnVo.success(ReturnEnum.SUCCESS.getMessage(), retForm);
    }

    /**
     * 添加会办节点，表单状态改为会办中
     */
    @RequestMapping("/setNode")
    public ReturnVo setNode(@RequestBody FormNodes formNodes) {
        ReturnVo returnVo = new ReturnVo();
        List<FormNode> formNodeList = formService.setNodes(
                formNodes.getFormId(), formNodes.getNodeList());
        if (null == formNodeList) {
            return returnVo.failure(ReturnEnum.ERROR.getMessage());
        }
        return returnVo.success(ReturnEnum.SUCCESS.getMessage(), formNodeList);
    }

    /**
     * 撤回表单，表单状态改为草稿
     */
    @RequestMapping("/back")
    public ReturnVo back(@RequestParam Long formId) {
        ReturnVo returnVo = new ReturnVo();
        FormMain retForm = formService.updateStatus(formId, FormMainStatusEnum.DRAFT.getCode());
        if (null == retForm) {
            return returnVo.failure(ReturnEnum.ERROR.getMessage());
        }
        return returnVo.success(ReturnEnum.SUCCESS.getMessage(), retForm);
    }

    /**
     * 查询表单
     */
    @RequestMapping("/find")
    public ReturnVo find(@RequestBody FindForm findForm) {
        ReturnVo returnVo = new ReturnVo();
        List<FormMain> formList = formService.findFormList(findForm.getStatus(),
                findForm.getCondition(),
                findForm.getPage(),
                findForm.getSize());
        return returnVo.success(ReturnEnum.SUCCESS.getMessage(), formList);
    }
}
